﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace DoAn
{
    public partial class frThemHang : Form
    {
        public frThemHang()
        {
            InitializeComponent();
        }

        private void frThemHang_Load(object sender, EventArgs e)
        {
            List<LoaiHangDTO> listlh = new List<LoaiHangDTO>();
            LoaiHangBUS lh = new LoaiHangBUS();
            listlh = lh.LayLoaiHang();
            cbLoaiHang.DataSource = listlh;
            cbLoaiHang.DisplayMember = "TenLoaiHang";
            cbLoaiHang.ValueMember = "MaLoaiHang";

            List<NhaCCDTO> ListNhaCC = new List<NhaCCDTO>();
            NhaCCBUS ncc = new NhaCCBUS();
            ListNhaCC = ncc.LayNhaCC();
            cbNhaCC.DataSource = ListNhaCC;
            cbNhaCC.DisplayMember = "TenNCC";
            cbNhaCC.ValueMember = "MaNCC";
        }
        private bool ThemHang()
        {
            if (txtMaHang.Text == "")
                MessageBox.Show("Bạn chưa nhập Mã hàng.", "Lỗi");
            else if (txtTenHang.Text == "")
                MessageBox.Show("Bạn chưa nhập Tên Hàng.", "Lỗi");
            else
            {
                HangHoaBUS hh = new HangHoaBUS();
                HangHoaDTO hhDto = new HangHoaDTO();
                hhDto.MaHang = txtMaHang.Text.ToString();
                hhDto.TenHang = txtTenHang.Text.ToString();
                hhDto.MaLoaiHang = int.Parse(cbLoaiHang.SelectedValue.ToString());
                hhDto.MaNCC = int.Parse(cbNhaCC.SelectedValue.ToString());
                hhDto.SoLuong = 0;
                if (hh.ThemHangHoa(hhDto) == false)
                    MessageBox.Show("Mã hàng đã tồn tại.", "Lỗi");
                else
                    return true;
            }
            return false;
        }

        private void btnLuuVaTT_Click(object sender, EventArgs e)
        {
            if (ThemHang())
            {
                txtMaHang.Text = "";
                txtTenHang.Text = "";
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (ThemHang())
            {
                txtMaHang.Text = "";
                txtTenHang.Text = "";
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (ThemHang())
                {
                    txtMaHang.Text = "";
                    txtTenHang.Text = "";
                }
        }        
        private void txtTenHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (ThemHang())
                {
                    txtMaHang.Text = "";
                    txtTenHang.Text = "";
                }
        }

        private void cbLoaiHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (ThemHang())
                {
                    txtMaHang.Text = "";
                    txtTenHang.Text = "";
                }
        }

        private void cbNhaCC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (ThemHang())
                {
                    txtMaHang.Text = "";
                    txtTenHang.Text = "";
                }
        }

        
    }
}
